<?php
// Controller/Default.php

class Controller_Pic extends FLEA_Controller_Action
{
    function actionindex(){
        // $smarty =& $this->_getView();
        // $smarty->display('index.html');
    }
    function actionlistbyword(){
        $smarty =& $this->_getView();
        $word = $_GET['word'];
        if(empty($word)){
            js_alert('please input the word!','history.back();');
        }
        $table_pics =& FLEA::getSingleton('Table_tofowordpic');
        $pics = $table_pics->getpicbyword($word);
        $table_yinbiao = & FLEA::getSingleton('Table_word2yinbiao');
        $yinbiao = $table_yinbiao->getmeanbyword($word);
        $smarty->assign('pics', $pics);
        $smarty->assign('word', $yinbiao);
        $smarty->display('search_piclist.html');
    }
    function actioncreate(){
        if($this->_isPOST()){
            $uploader =& FLEA::getSingleton('FLEA_Helper_FileUploader');
            if ($uploader->isFileExist('pic')){
                $file =& $uploader->getFile('pic');
                if (!$file->check(FLEA::getAppInf('thumbFileExts'))) {
                    js_alert('不允许的文件类型', 'history.back();');
                }
                $filepath = $file->isMoved()?$file->getNewPath():$file->getTmpName();
                $row['Type'] = $file->getExt();
                $row['PicBuf'] = file_get_contents($filepath);
            }
            $row['Word'] = $_POST['word'];
            $row['rank'] = empty($_POST['rank'])?100:$_POST['rank'];
            if(empty($row['Word'])){
                js_alert('请输入单词!','history.back();');
            }
            $table_pic =& FLEA::getSingleton('Table_tofowordpic');
            $no = $table_pic->findCount(array('Word'=>$row['Word']));
            $row['No'] = ++$no;
            if(!$table_pic->create($row)){
                js_alert('新建图片出错,请联系管理员','history.back();');
            }
            $text['Word'] = $row['Word'];
            $text['No'] = $row['No'];
            $text['pic_text'] = $_POST['pic_text'];
            $text['pic_text_zh'] = $_POST['pic_text_zh'];
            $table_text =& FLEA::getSingleton('Table_tofowordtext');
            if($table_text->save($text)){
                js_alert('更新成功');
            }
            else{
                js_alert('更新失败，请重试或联系管理员','history.back();');
            }
        }
        else{
            $smarty =& $this->_getView();
            $smarty->display('createpic.html');
        }
    }
    function actionedit(){
        if($this->_isPOST()){
            $uploader =& FLEA::getSingleton('FLEA_Helper_FileUploader');
            if ($uploader->isFileExist('pic')) {
                $file =& $uploader->getFile('pic');
                if (!$file->check(FLEA::getAppInf('thumbFileExts'))) {
                    js_alert('不允许的文件类型', 'history.back();');
                }
                $filepath = $file->isMoved()?$file->getNewPath():$file->getTmpName();
                $row['Type'] = $file->getExt();
                $row['PicBuf'] = file_get_contents($filepath);
            }
            $row['id'] = (int)$_POST['id'];
            $row['Word'] = $_POST['word'];
            $row['rank'] = $_POST['rank'];
            $row['No'] = (int)$_POST['no'];
            $text['id'] = (int)$_POST['text_id'];
            $text['pic_text'] = $_POST['pic_text'];
            $text['pic_text_zh'] = $_POST['pic_text_zh'];
            $table_text =& FLEA::getSingleton('Table_tofowordtext');
            $table_pic =& FLEA::getSingleton('Table_tofowordpic');
            if($table_pic->save($row) && $table_text->save($text)){
                js_alert('更新成功','history.back();');
            }
            else{
                js_alert('更新失败，请重试或联系管理员','history.back();');
            }
        }
        else{
            $id = $_GET['id'];
            if(empty($id)){
                js_alert('please input the word!','history.back();');
            }
            $smarty =& $this->_getView();
            $table_pic =& FLEA::getSingleton('Table_tofowordpic');
            $pic = $table_pic->find((int)$id);
            unset($pic['PicBuf']);
            $table_text =& FLEA::getSingleton('Table_tofowordtext');
            $text = $table_text->find(array('Word'=>$pic[Word],'No'=>(int)$pic[No]));
            $pic['text_id'] = $text['id'];
            $pic['pic_text'] = trim($text['pic_text']);
            $pic['pic_text_zh'] = $text['pic_text_zh'];
            unset($text);
            $smarty->assign('pic', $pic);
            $smarty->display('editpic.html');
        }
    }
     function actionlist(){
        $smarty =& $this->_getView();
        $page= (int)$_GET['page'];
        if(empty($page)){
            $page = 1; 
        }
        $table_pics =& FLEA::getSingleton('Table_tofowordpic');
        FLEA::loadHelper('Pager');
        $pager =& new FLEA_Helper_Pager($table_pics,$page,20,array('status'=>1),null,1);

        // select the word category
        $worddb = $_SESSION['word_db'];
        if(!empty($worddb)){
            $table_words =& FLEA::getSingleton('Table_'.$worddb);
            $words = $table_words->findAll(null,null,null,'Word');
            foreach($words as & $value){
                $value = $value['Word'];
            }
            $pager->_conditions = array('in()'=>array('Word'=>$words),'status'=>1);
            $pager->setCount($table_pics->findCount($pager->_conditions));
        }

        $data = $pager->findAll('id,Word,No,Type,rank');
        $data_page = $pager->getPagerData();
        unset($data_page['pagesNumber']);
        $smarty->assign('data', $data);
        $smarty->assign('data_page', $data_page);
        $smarty->display('piclist.html');
    }
    function actionlistintrash(){
        $smarty =& $this->_getView();
        $page= (int)$_GET['page'];
        if(empty($page)){
            $page = 1; 
        }
        $table_pics =& FLEA::getSingleton('Table_tofowordpic');
        FLEA::loadHelper('Pager');
        $pager =& new FLEA_Helper_Pager($table_pics,$page,20,array('status'=>0),null,1);
        $data = $pager->findAll('id,Word,No,Type,rank');
        $data_page = $pager->getPagerData();
        unset($data_page['pagesNumber']);
        $smarty->assign('data', $data);
        $smarty->assign('data_page', $data_page);
        $smarty->display('picintrash.html');
    }
     function actiondelintrash(){
        $smarty =& $this->_getView();
        $row['id']= (int)$_GET['id'];
        if(empty($row['id'])){
            js_alert("链接错误!",'history.back();');
        }
        $table_pics =& FLEA::getSingleton('Table_tofowordpic');
        $table_text =& FLEA::getSingleton('Table_tofowordtext');
        $table_voice =& FLEA::getSingleton('Table_tofowordvoice');
        $data = $table_pics->find($row['id']);
        $tmp = array('Word'=>$data['Word'],'No'=>$data['No']);
        if($table_pics->remove($row)){
            $table_text->removeByconditions($tmp)&&$table_voice->removeByconditions($tmp);
            js_alert('彻底删除!','window.close();');
        }
        else{
            js_alert('出错了，请联系管理员','history.back();');
        }
    }
    function actiondel(){
        $smarty =& $this->_getView();
        $row['id']= (int)$_GET['id'];
        if(empty($row['id'])){
            js_alert("链接错误!",'history.back();');
        }
        $table_pics =& FLEA::getSingleton('Table_tofowordpic');
        $row['status'] = 0;
        if($table_pics->save($row)){
            js_alert('成功移除到回收站','window.close();');
        }
        else{
            js_alert('出错了，请联系管理员','history.back();');
        }
     }
    function actionbackfromtrash(){
        $smarty =& $this->_getView();
        $row['id']= (int)$_GET['id'];
        if(empty($row['id'])){
            js_alert("链接错误!",'history.back();');
        }
        $table_pics =& FLEA::getSingleton('Table_tofowordpic');
        $row['status'] = 1;
        if($table_pics->save($row)){
            js_alert('从回收站中恢复','window.close();');
        }
        else{
            js_alert('出错了，请联系管理员','history.back();');
        }
    }
  function actionvoice(){
        $word = $_GET[word];
        $no = $_GET[no];
        $table_voice = & FLEA::getSingleton('Table_tofowordvoice');
        $voice = $table_voice->find(array('Word'=>$word,'No'=>$no));
        Header("Content-type:audio/mp3"); 
        print $voice[VoiceData];
    }
 }
